package com.springboot.zookeeper.controller;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author 贝壳
 * @date 2021/7/22 4:36 下午
 */
@RestController
@RequestMapping("/")
public class IndexController {
    private final CuratorFramework curatorFramework;

    public IndexController(CuratorFramework curatorFramework) {
        this.curatorFramework = curatorFramework;
    }

    @GetMapping("query_zk_notes")
    public String queryZkNotes(@RequestParam(value = "path") String path) {
        try {
            List<String> nodes =  curatorFramework.getChildren().forPath(path);
            return nodes.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @GetMapping("delete_zk_notes")
    public String deleteZkNotes(@RequestParam(value = "path") String path) {
        try {
            curatorFramework.delete().deletingChildrenIfNeeded().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "success";
    }

}
